/*
 * ConsultarAgendamento.java
 *
 * Created on 14/04/2011, 18:02:38
 */

package pbf.internalframe;

import calendario.InterFaceCalendario;
import java.util.ArrayList;
import java.util.List;
import javax.swing.table.DefaultTableModel;
import org.hibernate.Session;
import pbf.conf.hibernate.HibernateUtil;
import pbf.conf.hibernate.entidades.Agendamento;
import pbf.util.DataHora;

/**
 *
 * @author Ricardo
 */
public class ConsultarAgendamento extends javax.swing.JInternalFrame
{
    private static List<Agendamento> listaAgendamentos = null;
    private static Session sessao = null;

    public ConsultarAgendamento() {
        initComponents();
        setAgendamentoDtAtendimento(DataHora.getDataPresente());
    }

    public void setVisible()
    {
        setAgendamentoDtAtendimento(DataHora.getDataPresente());
        setTable();
        setVisible(true);
    }

    private void setTable() {
        sessao = HibernateUtil.openSession();
        sessao.beginTransaction();

        DefaultTableModel model = (DefaultTableModel) tabela.getModel();
        model.setNumRows(0);
        
        listaAgendamentos = (ArrayList<Agendamento>) sessao.createQuery("From pbf.conf.hibernate.entidades.Agendamento WHERE AGENDAMENTO_ATIVO='S' AND (AGENDAMENTO_DT_ATENDIMENTO BETWEEN '"+DataHora.converteStringData(agendamentoDtAtendimento.getText())+" 00:00:00 ' AND '"+DataHora.converteStringData(agendamentoDtAtendimento.getText())+" 23:59:59') ORDER BY AGENDAMENTO_AT_PREFERENCIAL ASC").list();
        Object[] objetos = new Object[3];

        int i = 1;
        for (Agendamento f : listaAgendamentos) {
            objetos[0] = i++;
            objetos[1] = f.getAgendamentoId();
            objetos[2] = f.getFamilia().getFamiliaNome();

            model.addRow(objetos);
        }
        sessao.getTransaction().commit();
        sessao.close();
    }

    private void setAgendamentoDtAtendimento(String data)
    {
        agendamentoDtAtendimento.setText(data);
    }

    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        agendamentoDtAtendimento = new javax.swing.JLabel();
        botaoCalendario = new javax.swing.JLabel();
        jLabel17 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        tabela = new javax.swing.JTable();
        jButton1 = new javax.swing.JButton();
        jSeparator1 = new javax.swing.JSeparator();

        setClosable(true);
        setDefaultCloseOperation(javax.swing.WindowConstants.HIDE_ON_CLOSE);
        setResizable(true);
        getContentPane().setLayout(null);

        agendamentoDtAtendimento.setFont(new java.awt.Font("Tahoma", 0, 16));
        agendamentoDtAtendimento.setHorizontalAlignment(javax.swing.SwingConstants.RIGHT);
        agendamentoDtAtendimento.setText("12/05/2011");
        getContentPane().add(agendamentoDtAtendimento);
        agendamentoDtAtendimento.setBounds(190, 20, 90, 20);

        botaoCalendario.setHorizontalAlignment(javax.swing.SwingConstants.CENTER);
        botaoCalendario.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/calendar-month.png"))); // NOI18N
        botaoCalendario.setVerticalAlignment(javax.swing.SwingConstants.BOTTOM);
        botaoCalendario.setCursor(new java.awt.Cursor(java.awt.Cursor.HAND_CURSOR));
        botaoCalendario.setHorizontalTextPosition(javax.swing.SwingConstants.LEFT);
        botaoCalendario.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                botaoCalendarioMouseClicked(evt);
            }
            public void mouseEntered(java.awt.event.MouseEvent evt) {
                botaoCalendarioMouseEntered(evt);
            }
            public void mouseExited(java.awt.event.MouseEvent evt) {
                botaoCalendarioMouseExited(evt);
            }
        });
        getContentPane().add(botaoCalendario);
        botaoCalendario.setBounds(280, 10, 30, 30);

        jLabel17.setFont(new java.awt.Font("Tahoma", 0, 16));
        jLabel17.setText("Data do agendamento:");
        getContentPane().add(jLabel17);
        jLabel17.setBounds(20, 20, 170, 20);

        tabela.setFont(new java.awt.Font("Tahoma", 0, 14));
        tabela.setModel(new javax.swing.table.DefaultTableModel(
            new Object [][] {
                {"1", "1985", "Ricardo Tavares Antunes de Oliveira"},
                {null, null, null},
                {null, null, null},
                {null, null, null}
            },
            new String [] {
                "Nº", "ID", "Nome do Responsável"
            }
        ) {
            boolean[] canEdit = new boolean [] {
                false, false, false
            };

            public boolean isCellEditable(int rowIndex, int columnIndex) {
                return canEdit [columnIndex];
            }
        });
        jScrollPane1.setViewportView(tabela);
        tabela.getColumnModel().getColumn(0).setPreferredWidth(50);
        tabela.getColumnModel().getColumn(0).setMaxWidth(50);
        tabela.getColumnModel().getColumn(1).setPreferredWidth(50);
        tabela.getColumnModel().getColumn(1).setMaxWidth(50);
        tabela.getColumnModel().getColumn(2).setMinWidth(150);

        getContentPane().add(jScrollPane1);
        jScrollPane1.setBounds(20, 50, 520, 280);

        jButton1.setIcon(new javax.swing.ImageIcon(getClass().getResource("/pbf/imagens/cross-circle.png"))); // NOI18N
        jButton1.setText("Fechar");
        jButton1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton1ActionPerformed(evt);
            }
        });
        getContentPane().add(jButton1);
        jButton1.setBounds(440, 360, 100, 25);
        getContentPane().add(jSeparator1);
        jSeparator1.setBounds(0, 350, 570, 10);

        java.awt.Dimension screenSize = java.awt.Toolkit.getDefaultToolkit().getScreenSize();
        setBounds((screenSize.width-572)/2, (screenSize.height-435)/2, 572, 435);
    }// </editor-fold>//GEN-END:initComponents

    private void botaoCalendarioMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoCalendarioMouseClicked
        InterFaceCalendario calendario = new InterFaceCalendario(null, true);
        calendario.setVisible(true);
        agendamentoDtAtendimento.setText(calendario.getData());
        setTable();
}//GEN-LAST:event_botaoCalendarioMouseClicked

    private void botaoCalendarioMouseEntered(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoCalendarioMouseEntered
        botaoCalendario.setBorder(javax.swing.BorderFactory.createBevelBorder(javax.swing.border.BevelBorder.RAISED));
}//GEN-LAST:event_botaoCalendarioMouseEntered

    private void botaoCalendarioMouseExited(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_botaoCalendarioMouseExited
        botaoCalendario.setBorder(null);
}//GEN-LAST:event_botaoCalendarioMouseExited

    private void jButton1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton1ActionPerformed
    setVisible(false);
    }//GEN-LAST:event_jButton1ActionPerformed


    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JLabel agendamentoDtAtendimento;
    private javax.swing.JLabel botaoCalendario;
    private javax.swing.JButton jButton1;
    private javax.swing.JLabel jLabel17;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JSeparator jSeparator1;
    public static javax.swing.JTable tabela;
    // End of variables declaration//GEN-END:variables

}
